package com.shujia.sql

import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment

object Demo3FlinkOnFileSystem {
  def main(args: Array[String]): Unit = {

    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //使用blink的计划器
      .inStreamingMode() //使用流模型
      .build()

    //窗口table 环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)


    /**
      * 基于文件创建动态表----有界流
      *
      */
    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE student_filesystem (
        |  id STRING,
        |  name STRING,
        |  age BIGINT,
        |  gender STRING,
        |  clazz STRING
        |)  WITH (
        |  'connector' = 'filesystem',
        |  'path' = 'data/students.json',
        |  'format' = 'json'
        |)
        |
      """.stripMargin)


    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE student_mysql (
        |  id STRING,
        |  name STRING,
        |  age BIGINT,
        |  gender STRING,
        |  clazz STRING,
        |  PRIMARY KEY (id) NOT ENFORCED
        |) WITH (
        |   'connector' = 'jdbc',
        |   'url' = 'jdbc:mysql://master:3306/student?useUnicode=true&characterEncoding=utf-8',
        |   'table-name' = 'student',
        |   'username' ='root',
        |   'password'='123456'
        |)
        |
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |insert into student_mysql
        |select * from student_filesystem
        |
      """.stripMargin)


  }

}
